import 'package:flutter_admin/utils/utils.dart';

class MrpProduction {
  String? id;
  String? reportdate;
  String? stationId;
  String? workshopId;
  String? qualifiedquantity;
  String? unit;
  String? productionorderno;
  String? defaultCode;
  String? productId;
  String? batchno;
  String? dateStart;
  String? dateFinished;
  String? productName;
  String? productCode;
  String? name;
  int? productQty;
  String? productUom;
  String? state;
  String? productType;
  String? productDesc;
  String? priceOrigin;
  num? price;
  String? priceCreateDate;

  MrpProduction({
    this.id,
    this.reportdate,
    this.stationId,
    this.workshopId,
    this.qualifiedquantity,
    this.unit,
    this.productionorderno,
    this.defaultCode,
    this.productId,
    this.batchno,
    this.dateStart,
    this.dateFinished,
    this.productName,
    this.productCode,
    this.name,
    this.productQty,
    this.productUom,
    this.state,
    this.productType,
    this.productDesc,
    this.priceOrigin,
    this.price,
    this.priceCreateDate,
  });

  MrpProduction copyWith({
    String? id,
    String? reportdate,
    String? stationId,
    String? workshopId,
    String? qualifiedquantity,
    String? unit,
    String? productionorderno,
    String? defaultCode,
    String? productId,
    String? batchno,
  }) {
    return new MrpProduction(
      id: id ?? this.id,
      reportdate: reportdate ?? this.reportdate,
      stationId: stationId ?? this.stationId,
      workshopId: workshopId ?? this.workshopId,
      qualifiedquantity: qualifiedquantity ?? this.qualifiedquantity,
      unit: unit ?? this.unit,
      productionorderno: productionorderno ?? this.productionorderno,
      defaultCode: defaultCode ?? this.defaultCode,
      productId: productId ?? this.productId,
      batchno: batchno ?? this.batchno,
    );
  }

  @override
  String toString() {
    return '''MrpProduction{
      id: id,
      reportdate: reportdate,
      stationId: stationId,
      workshopId: workshopId,
      qualifiedquantity: qualifiedquantity,
      unit: unit,
      productionorderno: productionorderno,
      defaultCode: defaultCode,
      productId: productId,
      batchno: batchno
      dateStart: $dateStart,
      dateFinished: $dateFinished,
      state: $state,
    }''';
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      (other is MrpProduction &&
          runtimeType == other.runtimeType &&
          id == other.id);

  factory MrpProduction.fromMap(Map<String, dynamic> map) {
    return new MrpProduction(
      reportdate: map['reportdate'] != false
          ? Utils.getDate(date: map['reportdate'].toString())
          : "",
      stationId: map['station_id'] != false
          ? map['station_id'].toString() as String?
          : "",
      workshopId: map['workshop_id'] != false
          ? map['workshop_id'].toString() as String?
          : "",
      qualifiedquantity: map['qualifiedquantity'] != false
          ? map['qualifiedquantity'].toString() as String?
          : "",
      unit: map['unit'] != false ? map['unit'].toString() as String? : "",
      productionorderno: map['productionorderno'] != false
          ? map['productionorderno'].toString() as String?
          : "",
      defaultCode: map['default_code'] != false
          ? map['default_code'].toString() as String?
          : "",
      productId: map['product_id'] != false
          ? map['product_id'].toString() as String?
          : "",
      batchno:
          map['batchno'] != false ? map['batchno'].toString() as String? : "",
    );
  }
  factory MrpProduction.fromMapCheck(Map<String, dynamic> map) {
    return new MrpProduction(
      id: map['id'] != false ? map['id'].toString() as String? : "",
      name: map['name'] != false ? map['name'].toString() as String? : "",
      productName: map['product_tmpl_id'] != false
          ? map['product_tmpl_id']['name'].toString() as String?
          : "",
      productCode: map['product_tmpl_id'] != false
          ? map['product_tmpl_id']['default_code'].toString() as String?
          : "",
      productUom: map['product_tmpl_id'] != false
          ? map['product_tmpl_id']['unit'].toString() as String?
          : "",
      productQty: map['product_uom_qty'] != false ? map['product_uom_qty'] : 0,
      state: map['state'] != false ? map['state'].toString() as String? : "",
      dateStart: (map['date_planned_start'] != false &&
              map['date_planned_start'] != null)
          ? map['date_planned_start'].toString() as String?
          : "",
      dateFinished: (map['date_planned_finished'] != false &&
              map['date_planned_finished'] != null)
          ? map['date_planned_finished'].toString() as String?
          : "",
    );
  }
  factory MrpProduction.fromPriceMap(Map<String, dynamic> map) {
    return new MrpProduction(
      id: map['id'] != false ? map['id'].toString() as String? : "",
      productName: map['product_name'] != false
          ? map['product_name'].toString() as String?
          : "",
      productCode: map['product_default_code'] != false
          ? map['product_default_code'].toString() as String?
          : "",
      productDesc: map['product_desc'] != false
          ? map['product_desc'].toString() as String?
          : "",
      price: map['price'] != false ? map['price'] : null,
      priceCreateDate: (map['price_create_date'] != false &&
              map['price_create_date'] != null)
          ? map['price_create_date'].toString() as String?
          : "",
      priceOrigin: map['price_origin'] != false
          ? map['price_origin'].toString() as String?
          : "",
    );
  }
  Map<String, dynamic> toMap() {
    // ignore: unnecessary_cast
    return {
      'id': this.id,
      'reportdate': this.reportdate,
      'station_id': this.stationId,
      'workshop_id': this.workshopId,
      'qualifiedquantity': this.qualifiedquantity,
      'unit': this.unit,
      'productionorderno': this.productionorderno,
      'default_code': this.defaultCode,
      'product_id': this.productId,
      'batchno': this.batchno
    } as Map<String, dynamic>;
  }

  List toDomainList() {
    List list = [
      if (this.productionorderno != null && this.productionorderno != '')
        ['name', '=', this.productionorderno],
      if (this.batchno != null && this.batchno != '')
        ['batchno', 'like', '%${this.batchno}%'],
      if (this.productId != null && this.productId != '')
        ['product_name', 'like', '%${this.productId}%'],
    ] as List;
    // if (list.length == 0) {
    //   list.add(['id', '!=', null]);
    // }
    // ignore: unnecessary_cast
    return list;
  }

  List toDomainListCheck() {
    List list = [
      // if (this.dateStart != null && this.dateStart != '')
      //   [
      //     'start',
      //     '>=',
      //     Utils.timeUTC(this.dateStart! + " 00:00:00", isToBeijing: false)
      //   ],
      // if (this.dateFinished != null && this.dateFinished != '')
      //   [
      //     'end',
      //     '<=',
      //     Utils.timeUTC(this.dateFinished! + " 23:59:59", isToBeijing: false)
      //   ],
      if (this.dateStart != null && this.dateStart != '')
        ['start', '=', this.dateStart],
      if (this.dateFinished != null && this.dateFinished != '')
        ['end', '=', this.dateFinished],
      //生产单号
      if (this.name != null && this.name != '')
        ['name', 'like', '%${this.name}%'],
      //产品名称
      if (this.productName != null && this.productName != '')
        ['product', 'like', '%${this.productName}%'],
      ['state', '!=', 'draft']
    ] as List;
    return list;
  }
}
